<?php
namespace apps\app;
if( ! APP_ROOT  )
    return 'Direct Access Deny!';

use Phalcon\Mvc\ModuleDefinitionInterface;
use Phalcon\Mvc\View\Engine\Volt as VoltEngine;
use Phalcon\Events\Manager as EventsManager;
use apps\app\listeners\DispatcherListener;

class Module implements ModuleDefinitionInterface
{

    public function registerAutoloaders( \Phalcon\DiInterface $di = null )
    {
        $loader = new \Phalcon\Loader();

        $loader->registerNamespaces( array(
            'apps\app\controllers' => APP_ROOT . 'apps/app/controllers/',
            'apps\app\models'      => APP_ROOT . 'apps/app/models/',
            'apps\app\plugins'     => APP_ROOT . 'apps/app/plugins/',
            'apps\app\enums'       => APP_ROOT . 'apps/app/enums/',
            'apps\app\bizs'        => APP_ROOT . 'apps/app/bizs/',
        	'apps\app\listeners'	=> APP_ROOT . 'apps/app/listeners/',
        	'apps\app\libraries'	=> APP_ROOT . 'apps/app/libraries/',
        ) );

        $loader->register();
    }

    /**
     * Register the services here to make them general or register in the ModuleDefinition to make them module-specific
     */
    public function registerServices( \Phalcon\DiInterface $di = null )
    {
        // Registering a dispatcher
        $di->set( 'dispatcher', function ( )
        {

            $dispatcher = new \Phalcon\Mvc\Dispatcher();

            // Attach a event listener to the dispatcher
            $eventManager = new \Phalcon\Events\Manager();
            // $eventManager->attach('dispatch', new \Acl( 'admin' ));
            
            $eventManager->attach( 'dispatch', new DispatcherListener() );
            
//            $eventManager->attach('dispatch:beforeExecuteRoute', new CacheEnablerPlugin($event, $dispatcher));
            $dispatcher->setEventsManager( $eventManager );
            $dispatcher->setDefaultNamespace( "apps\\app\\controllers\\" );
            return $dispatcher;
        } );

        // Registering the view component
        $di->set( 'view', function(){
            $view = new \Phalcon\Mvc\View();
            $view->setViewsDir( APP_ROOT . 'apps/app/views/' );
            $view->registerEngines( array(
                '.volt' => function( $view, $di )
                {
                    $volt = new VoltEngine( $view, $di );
                    $volt->setOptions( array(
                        'compiledPath'      => APP_ROOT . 'apps/app/cache/',
                        'compiledSeparator' => '_',
                        'compileAlways'     => true ) );
                    return $volt;
                } )
                //'.phtml' => 'Phalcon\Mvc\View\Engine\Php'
            );
            return $view;
        } );
        
        $di->set( 'db',
    		function ()
    		{
    			$db = new \Phalcon\Db\Adapter\Pdo\Mysql( array(
    					'adapter'  => $this->dbCfg->db->adapter,
    					'host'     => $this->dbCfg->db->host,
    					'username' => $this->dbCfg->db->username,
    					'password' => $this->dbCfg->db->password,
    					'dbname'   => $this->dbCfg->db->dbname,
    					'charset'  => $this->dbCfg->db->charset
    			) );
    
//     			$eventsMgr = new EventsManager();
//     			$dbListener = new \apps\home\listeners\DbListener();
    
//     			$eventsMgr->attach( 'db', $dbListener );
    
//     			$db->setEventsManager( $eventsMgr );
    
    			return $db;
    		}, true );
   }

}
    
